
/*
* 提交评论，这里设置一个type，
* 1：回复问题
* 2：回复评论
* */
function comment2Target(targetId,type,content) {
    if (!content) {
        alert("不能回复空内容～～");
        return;
    }

    $.ajax({
        type:'POST',
        url:'/comment',
        contentType: 'application/json',
        data: JSON.stringify({
            "parentId":targetId,
            "content":content,
            "type":type
        }),
        success: function (response) {
            var isReload = true;
            if (response.code == 200) {
                $("#comment_section").hide();
            } else {
                if (response == 2003) {
                    var isAccept = confirm(response.message());
                    window.open("https://gitee.com/oauth/authorize?client_id=e42422245f84e7d42d1e9a301a1940e3ea3e46729373dbc2ff41d5625c24d749&redirect_uri=http://localhost:8887/callback&response_type=code&scope=user_info&state=1")
                    window.localStorage.setItem("closable",true)
                    isReload = flase;
                }else{
                    alert(response.message);
                }
            }
            if (isReload) {
                window.location.reload();
            }
        },
        dataType: "json"
    });
}


/*
* 回复问题，第一模版，未和回复评论抽取成共同方法时
* */
function posttemp() {
    var questionId =$("#question_id").val();
    var content = $("#comment_content").val();

    $.ajax({
        type: 'POST',
        url: '/comment',
        contentType: 'application/json',
        data: JSON.stringify({
            "parentId":questionId,
            "content":content,
            "type":1
        }),
        success: function (response) {
            var isReload = true;
            if (response.code == 200) {
                $("#comment_section").hide();
            }else {
                if (response.code = 2003) {
                    var isAccept = confirm(response.message);
                    window.open("https://gitee.com/oauth/authorize?client_id=e42422245f84e7d42d1e9a301a1940e3ea3e46729373dbc2ff41d5625c24d749&redirect_uri=http://localhost:8887/callback&response_type=code&scope=user_info&state=1")
                    window.localStorage.setItem("closable",true)
                    isReload = flase;
                }else{
                    alert(response.message);
                }
            }
            console.log(response);
            if (isReload) {
                window.location.reload();
            }
        },
        dataType: "json"
    });

}

/*
* 回复问题
* */
function post(e) {
    var id = $("#question_id").val();
    var content = $("#comment_content").val();
    comment2Target(id, 1, content);

}

/*
* 回复评论
* */
function comment(e) {
    debugger;
    var id = e.getAttribute("data-id");
    var content = $("#input-" + id).val();
    comment2Target(id, 2, content);
}



/*
* 展开二级评论
* */
function collapseComments(e) {
    debugger;
    var id = e.getAttribute("data-id");
    var comments = $("#comment-" + id);

    // 获取一下二级评论的展开状态
    var collapse = e.getAttribute("data-collapse");
    if (collapse) {
        // 折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else {
        var subCommentContainer = $("#comment-" + id);
        // subCommentContainer.children().length != 1
        var aa = false
        if (subCommentContainer.children().length != 1) {
            //展开二级评论
            comments.addClass("in");
            // 标记二级评论展开状态
            e.setAttribute("data-collapse", "in");
            e.classList.add("active");
        } else {
            $.getJSON(
                "/comment/" + id,
                function (data) {
                    //console.log(data);
                    $.each(data.data.reverse(), function (index, comment) {
                        var mediaLeftElement = $("<div/>", {
                            "class": "media-left"
                        }).append($("<img/>", {
                            "class": "media-object img-rounded",
                            "src": comment.user.avatarUrl
                        }));

                        var mediaBodyElement = $("<div/>", {
                            "class": "media-body"
                        }).append($("<h5/>", {
                            "class": "media-heading",
                            "html": comment.user.name
                        })).append($("<div/>", {
                            "html": comment.content
                        })).append($("<div/>", {
                            "class": "menu"
                        }).append($("<span/>", {
                            "class": "pull-right",
                            "html": moment(comment.gmtCreate).format('YYYY-MM-DD')
                        })));

                        var mediaElement = $("<div/>", {
                            "class": "media"
                        }).append(mediaLeftElement).append(mediaBodyElement);

                        var commentElement = $("<div/>", {
                            "class": "col-lg-12 col-md-12 col-sm-12 col-xs-12 comments"
                        }).append(mediaElement);

                        subCommentContainer.prepend(commentElement);
                    });
                    //展开二级评论
                    comments.addClass("in");
                    // 标记二级评论展开状态
                    e.setAttribute("data-collapse", "in");
                    e.classList.add("active");
                });
            // comments.addClass("in");
            // // 标记二级评论展开状态
            // e.setAttribute("data-collapse", "in");
            // e.classList.add("active");
        }
    }
}
function showSelectTag(){
    $("#select-tag").show()
}

function selectTag(e) {
    var value = e.getAttribute("data-tag");
    var previous = $("#tag").val();

    if (previous) {
        var index = 0;
        var appear = false;//记录value是否已经作为一个独立的标签出现过
        while (true) {
            //value字符串在previous中出现的位置
            index = previous.indexOf(value, index);
            if (index == -1) {
                break;
            }
            if ((index == 0 || previous.charAt(index - 1) == ",")
                && (index + value.length == previous.length || previous.charAt(index + value.length) == ",")
            ) {
                appear = true;
                break;
            }
            index++;//用于搜索下一个出现位置
        }
        if (!appear) {
            $("#tag").val(previous + "," + value);
        }
    } else {
        $("#tag").val(value);
    }
}